package com.example.test_witns.ui.sheets;

import android.app.Dialog;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.test_witns.R;
import com.example.test_witns.pojo.TodoTask;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

public class TaskBottomSheet extends BottomSheetDialogFragment {
    
    private static final String ARG_TASK = "task";
    private final TodoTask currentTask = new TodoTask("null", "null", 0, "null", false, "", "", "");
    private OnTaskActionListener listener;

    public static TaskBottomSheet newInstance(TodoTask task) {
        Bundle args = new Bundle();
        args.putString("title",task.getTitle());
        args.putString("category",task.getCategory());
        args.putString("priority",String.valueOf(task.getPriority()));
        args.putString("dueDate", task.getDueDate());
        args.putString("id", String.valueOf(task.getId()));
        args.putString("notes", task.getNotes());
        args.putString("dueTime", task.getDueTime());
        TaskBottomSheet fragment = new TaskBottomSheet();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            currentTask.setTitle(getArguments().getString("title"));
            currentTask.setCategory(getArguments().getString("category"));
            currentTask.setPriority(Integer.parseInt(getArguments().getString("priority")));
            currentTask.setDueDate(getArguments().getString("dueDate"));
            currentTask.setId(Long.parseLong(getArguments().getString("id")));
            currentTask.setNotes(getArguments().getString("notes"));
            currentTask.setDueTime(getArguments().getString("dueTime"));
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.bottom_sheet_task, null);
        dialog.setContentView(view);

        // 初始化视图
        TextView tvTitle = view.findViewById(R.id.tv_sheet_title);
        TextView tvCategory = view.findViewById(R.id.tv_sheet_category);
        TextView tvPriority = view.findViewById(R.id.tv_sheet_priority);
        TextView tv_sheet_note = view.findViewById(R.id.tv_sheet_note);
        ImageView bottom_sheet_task_priority = view.findViewById(R.id.bottom_sheet_task_priority);
        
        // 填充数据
        tvTitle.setText(currentTask.getTitle());
        tvCategory.setText(currentTask.getCategory());
        tvPriority.setText(String.valueOf(currentTask.getPriority()));
        tv_sheet_note.setText(currentTask.getNotes());

        if (currentTask.getPriority() > 100 && currentTask.getPriority() < 201){
            bottom_sheet_task_priority.setImageTintList(ColorStateList.valueOf(Color.parseColor("#FA8C00")));
        } else if (currentTask.getPriority() < 101) {
            bottom_sheet_task_priority.setImageTintList(ColorStateList.valueOf(Color.GREEN));
        }else {
            bottom_sheet_task_priority.setImageTintList(ColorStateList.valueOf(Color.RED));
        }


        // 设置按钮点击
        view.findViewById(R.id.btn_edit).setOnClickListener(v -> {
            if (listener != null) {
                listener.onEditTask(currentTask);
            }
            dismiss();
        });

        view.findViewById(R.id.btn_delete).setOnClickListener(v -> {
            if (listener != null) {
                listener.onDeleteTask(currentTask);
            }
            dismiss();
        });

        return dialog;
    }

    public void setOnTaskActionListener(OnTaskActionListener listener) {
        this.listener = listener;
    }

    public interface OnTaskActionListener {
        void onEditTask(TodoTask task);
        void onDeleteTask(TodoTask task);
    }
}